﻿//
// *************************************************************************************
// WP-Framework
// 
// Developed by
// Lukas Kretschmar (lkretschmar.dev@bluewin.ch)
//
// Switzerland, Copyright (c) 2013 lkretschmar
// This content is released under the MIT License (http://opensource.org/licenses/MIT).
//     
// *************************************************************************************
//
//



using Microsoft.VisualStudio.TestTools.UnitTesting;
using KretschIT.WP_Fx.EasyMoq.Test.Helpers.Objects;
using KretschIT.WP_Fx.Testing;

namespace KretschIT.WP_Fx.EasyMoq.Test.IntegrationTests
{
    [TestClass]
    public class GenericActionTest
    {
        private IMock<ITestGenericMethod> mock;

        [TestInitialize]
        public void Init()
        {
            this.mock = Easy.Moq.Mock<ITestGenericMethod>();
        }

        [TestMethod]
        public void Test_Setup_Verify()
        {
            this.mock.Setup(m => m.Action<int>(1));
            this.mock.Verify(m => m.Action<int>(1), Condition.Never());
        }

        [TestMethod]
        public void Test_Setup_Execute_Verify()
        {
            this.mock.Setup(m => m.Action<int>(5));

            this.mock.Object.Action<int>(5);

            this.mock.Verify(m => m.Action<int>(5), Condition.Once());
        }

        [TestMethod]
        public void Test_Setup_Execute_Verify_WrongType()
        {
            this.mock.Setup(m => m.Action<int>(5));

            this.mock.Object.Action<string>("5");

            this.mock.Verify(m => m.Action<int>(5), Condition.Never());
            this.mock.Verify(m => m.Action<string>("5"), Condition.Once());
        }

        [TestMethod]
        public void Test_Setup_Execute_Verify_WrongMethod()
        {
            this.mock.Setup(m => m.Action<string>("hello"));

            this.mock.Object.Action("hello");

            this.mock.Verify(m => m.Action<string>("hello"), Condition.Never());
            this.mock.Verify(m => m.Action("hello"), Condition.Once());
        }

        [TestMethod]
        public void Test_Setup_ExecuteException_Verify()
        {
            this.mock.Setup(m => m.Action<int>(42)).Throws(new TestException());

            ExceptionAssert.Throws<TestException>(() =>
                {
                    this.mock.Object.Action<int>(42);
                });

            this.mock.Verify(m => m.Action<int>(42), Condition.Once());
        }

        [TestMethod]
        public void Test_Setup_ExecuteX_Verify()
        {
            this.mock.Setup(m => m.Action<int>(42));

            var obj = this.mock.Object;
            obj.Action<int>(42);
            obj.Action<int>(42);
            obj.Action<int>(42);
            obj.Action<int>(43);

            this.mock.Verify(m => m.Action<int>(42), Condition.Exactly(3));
            this.mock.Verify(m => m.Action<int>(43), Condition.AtLeastOnce());
        }
    }
}
